/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ipo.mm.ejb.util.UtilityBeanLocal;

/**
 *
 * @author makcro
 */
public class DocDownloadServlet extends HttpServlet {
    @EJB
    private UtilityBeanLocal utilityBeanBean;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        try {


            

            Connection conn = utilityBeanBean.getResourceJDBCConnection();
            
            String meetingId = request.getParameter("mid");
            String docId = request.getParameter("did");
            Blob fileFromDb = null;
            String fileName = null;

            String query = "SELECT D.File AS File, D.FileName AS FileName FROM DOCUMENT AS D WHERE D.ID = ? AND D.Meeting = ?";
            PreparedStatement pstmt = conn.prepareStatement(query);

            pstmt.setInt(1, Integer.parseInt(docId));
            pstmt.setInt(2, Integer.parseInt(meetingId));

            ResultSet resultSet = pstmt.executeQuery();

            while(resultSet.next()){
                fileFromDb = resultSet.getBlob("File");
                fileName = resultSet.getString("FileName");
            }

            response.setContentType(getServletContext().getMimeType(fileName));
            response.setHeader ("Content-Disposition", "attachment; filename=" + fileName);

            ServletOutputStream servOut = response.getOutputStream();
            InputStream fileInputStream = fileFromDb.getBinaryStream();

            int bufferSize = 1024;
            int lenghtRead = 0;
            byte[] buffer = new byte[bufferSize];

            while((lenghtRead = fileInputStream.read(buffer)) != -1 ){

                servOut.write(buffer, 0, lenghtRead);
            }

            resultSet.close();
            pstmt.close();
            conn.close();


        } catch (SQLException ex) {
            Logger.getLogger(DocDownloadServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

        }


        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
